# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        if not head:
            return None
        dummy = ListNode(0, head)
        pre = dummy
        cur = head
        while cur:
            if cur.val == val:
                pre.next = cur.next
                cur = cur.next
                continue
            pre = pre.next
            cur = cur.next
        return dummy.next